package com.winter.controller;

/**
 * Created by lenovo on 2016/12/6.
 */

import com.winter.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@EnableAutoConfiguration
public class ObjectRedisController {

    protected static Logger logger=LoggerFactory.getLogger(ObjectRedisController.class);

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    @Resource(name="redisTemplate")
    ValueOperations<Object,Object> valOps;

    @RequestMapping("/setPerson")
    public String set(String id,String name,Integer age){
        logger.debug("访问setPerson:id={},name={},age={}",id,name,age);
        User user=new User(id,name,age);
        valOps.set(user.getId(), user);
        return "Set person Ok";
    }

    @RequestMapping("/getPerson")
    @ResponseBody
    public User getUser(String id){
        logger.debug("访问getPerson:id={}",id);
        return (User) valOps.get(id);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ObjectRedisController.class, args);
    }

}